/*
 * 版权所有 (c) 华为技术有限公司 2022-2022
 */
package com.huawei.cameratest;

import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;

import static com.huawei.cameratest.MultiImageReaderActivity.isRunning;

public class StoreFileThread extends Thread {
    private String TAG = this.getClass().getSimpleName();
    private ArrayBlockingQueue<byte[]> H264BufferQueue;
    private File file;

    public StoreFileThread(File file, ArrayBlockingQueue<byte[]> H264BufferQueue) {
        this.H264BufferQueue = H264BufferQueue;
        this.file = file;
    }

    @Override
    public void run() {
        Log.i(TAG, "Start StoreFileThread");
        while (isRunning) {
            try {
                byte[] bytes = null;
                bytes = H264BufferQueue.take();
                try (FileOutputStream output = new FileOutputStream(file,true);
                BufferedOutputStream bos = new BufferedOutputStream(output)) {
                    bos.write(bytes, 0, bytes.length);
                    bos.flush();
                    bos.close();
                    output.close();
                }
            } catch (InterruptedException | IOException e) {
                e.printStackTrace();
            }
        }
    }
}
